package error

func deleteAndEarn(nums []int) int {
	tempMap := make(map[int]int)
	for _, num := range nums {
		tempMap[num]++
	}
	result := 0
	for i := 0; i < len(nums); i++ {
		copyMap := tempMap
		tempResult := 0
		deleteKey := nums[i]
		for len(copyMap) > 0 {
			if _, ok := copyMap[deleteKey-1]; ok {
				delete(copyMap, deleteKey-1)
			}
			if _, ok := copyMap[deleteKey+1]; ok {
				delete(copyMap, deleteKey+1)

			}
			for key, value := range copyMap {
				tempResult += key
				deleteKey = key
				if value == 1 {
					delete(copyMap, deleteKey)
				} else if value > 1 {
					copyMap[deleteKey]--
					if copyMap[deleteKey] == 0 {
						delete(copyMap, deleteKey)
					}
				}
				break
			}
		}
		result = max(result, tempResult)
	}
	return result
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
